import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { HeroService } from '../../../../services/hero.service';
import { NzMessageService,NzModalService } from 'ng-zorro-antd';
import {
  FormBuilder,
  FormControl,
  FormGroup,
  Validators
} from '@angular/forms';

@Component({
  selector: 'app-editprinter',
  templateUrl: './editprinter.component.html',
  styleUrls: ['./editprinter.component.css']
})
export class EditprinterComponent implements OnInit {

  validateForm: FormGroup;
  btn_loading=false;
  public name:string;
  public brandId:string;
  public brandName:string;
  public typeId:string;
  public typename:string;
  public radioValue:string;
  public tips:number;
  public brandlist:Array<any>=[];
  public typelist:Array<any>=[];
  formatterPercent = value => `${value} %`;
  parserPercent = value => value.replace(' %', '');

  submitForm(): void {
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[ i ].markAsDirty();
      this.validateForm.controls[ i ].updateValueAndValidity();
    }
  }

  constructor(private heroService: HeroService,
              private fb: FormBuilder,
              private router: Router,
              private route: ActivatedRoute,
              private message: NzMessageService,
              private modalService: NzModalService) { }

  ngOnInit(): void {
    const id = this.route.snapshot.paramMap.get('id');
    this.validateForm = this.fb.group({
      brandName: [ null ],
      typecode: [ null ],
      name: [ null ],
      ksum: [ null, [ Validators.required ] ],
      csum: [ null, [ Validators.required ] ],
      msum: [ null, [ Validators.required ] ],
      ysum: [ null, [ Validators.required ] ],
      kWarning: [ 10, [ Validators.required ] ],
      cWarning: [ 10, [ Validators.required ] ],
      mWarning: [ 10, [ Validators.required ] ],
      yWarning: [ 10, [ Validators.required ] ]
    });
    //获取品牌
    let url1=host+"/main/util/getBrand";
    this.heroService.get(url1).then(result=>{
        // console.log(result.data);
        if(result.code==0){
          this.brandlist=result.data;
        }else{
          this.modalService.error({
            nzTitle: '提示',
            nzContent: result.msg
          });
        }
    })
    let url=host+"/main/merchantPrinter/add?id="+id;
    this.heroService.get(url).then(result=>{
        if(result.code==0){
          this.brandId=String(result.data.bean.brandId);
          this.typeId=String(result.data.bean.typeId);
          this.brandName=result.data.bean.brandName;
          this.typename=result.data.bean.typeCode;
          if(result.data.bean.isSigned){
            this.validateForm.get('brandName').reset({value:this.brandId,disabled: true});
            let timer1 = setInterval( () => {
              if(this.typelist.length > 0){
                this.validateForm.get('typecode').reset({value:this.typeId,disabled: true});
                clearInterval(timer1);
              }
            });
            this.validateForm.get('name').reset({value:result.data.bean.name,disabled: true});
            this.name=result.data.bean.name;
          }else{
            this.validateForm.get('name').setValue(result.data.bean.name);
            this.validateForm.get('brandName').setValue(this.brandId);
            let timer2 = setInterval( () => {
              if(this.typelist.length > 0){
                this.validateForm.get('typecode').setValue(this.typeId);
                clearInterval(timer2);
              }
            });
          }
          this.validateForm.get('ksum').setValue(result.data.bean.kSum);
          this.validateForm.get('csum').setValue(result.data.bean.cSum);
          this.validateForm.get('msum').setValue(result.data.bean.mSum);
          this.validateForm.get('ysum').setValue(result.data.bean.ySum);
          this.validateForm.get('kWarning').setValue(result.data.printerSet.kWarning);
          this.validateForm.get('cWarning').setValue(result.data.printerSet.cWarning);
          this.validateForm.get('mWarning').setValue(result.data.printerSet.mWarning);
          this.validateForm.get('yWarning').setValue(result.data.printerSet.yWarning);
          if(result.data.bean.isAuto){
            this.radioValue="1";
          }else{
            this.radioValue="0";
          }
        }else{
          this.modalService.error({
            nzTitle: '提示',
            nzContent: result.msg
          });
        }
    })
  }
  //获取型号
  getTypecode(brand){
    for(let i in brand.listOfTemplateOption){
      if(brand.listOfTemplateOption[i].nzValue==brand.listOfSelectedValue){
        this.brandName=brand.listOfTemplateOption[i].nzLabel;
      }
    }
    let url=host+"/main/util/getTypesByBrand";
    let model="brandId="+brand.value;
    this.heroService.post(url,model).then(result=>{
        if(result.code==0){
          this.typelist=result.data;
          this.validateForm.get('typecode').setValue(String(this.typelist[0].id));
          this.tips=1;
        }else{
          this.modalService.error({
            nzTitle: '提示',
            nzContent: result.msg
          });
        }
    })
  }
  getTypelabel(type){
    setTimeout( () => {
      if(type.listOfTemplateOption[1].nzValue){
        for(let i in type.listOfTemplateOption){
          if(type.listOfTemplateOption[i].nzValue == type.listOfSelectedValue){
            this.typename=type.listOfTemplateOption[i].nzLabel;
          }
        }
        console.log(this.typename)
      }
    },500);
    for(let i in this.typelist){
      if(type.value == this.typelist[i].id){
        this.validateForm.get('ksum').setValue(this.typelist[i].ksum);
        this.validateForm.get('csum').setValue(this.typelist[i].csum);
        this.validateForm.get('msum').setValue(this.typelist[i].msum);
        this.validateForm.get('ysum').setValue(this.typelist[i].ysum);
      }
    }
  }
  editPrinter() {
    const id = this.route.snapshot.paramMap.get('id');
    if(!this.validateForm.value.brandName){
      this.validateForm.value.brandName=this.brandId;
    }
    if(!this.validateForm.value.typecode){
      this.validateForm.value.typecode=this.typeId;
    }
    if(!this.name){
      this.name=this.validateForm.value.name;
    }
    if(this.validateForm.status=="VALID"){
      this.btn_loading=true;
      let url=host+"/main/merchantPrinter/save";
      let model='id='+id+'&brandId='+this.validateForm.value.brandName+'&brandName='+this.brandName+'&typeId='+this.validateForm.value.typecode+'&typeCode='+this.typename+'&isAuto='+this.radioValue+'&name='+this.name+'&kSum='+this.validateForm.value.ksum+'&cSum='+this.validateForm.value.csum+'&mSum='+this.validateForm.value.msum+'&ySum='+this.validateForm.value.ysum+'&kWarning='+this.validateForm.value.kWarning+'&cWarning='+this.validateForm.value.cWarning+'&mWarning='+this.validateForm.value.mWarning+'&yWarning='+this.validateForm.value.yWarning;
      this.heroService.post(url,model).then(result=>{
          this.btn_loading=false;
          if(result.code==0){
            this.message.success('修改成功');
            this.router.navigate(['/main/printer']);
          }else{
            this.modalService.error({
              nzTitle: '提示',
              nzContent: result.msg
            });
          }
      })
    }
  }

  backPrevent(){
    return false;
  }
}
